﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class Card : MonoBehaviour
{
	CardFaceType _faceType;
	// card up = face_down , card lat = face_up
	public CardFaceType faceType {
		get { return _faceType; }
		set {
			_faceType = value;
			transform.DOScaleX (0.01F, 0.09F).SetUpdate (true).OnComplete (() => {
				if (value == CardFaceType.FACE_DOWN) {
					render.sprite = GameDefine.instance.GetCardModuleByType (CardType.RED).sprite;

				} else {
					render.sprite = GameDefine.instance.GetCardModuleByType (type).sprite;
				}
				transform.DOScaleX (1, 0.09F).SetUpdate (true);
			});
		}
	}

	int _iceCardLevel;
	// so turn xuat hien ice card
	public int iceCardLevel {
		get { return _iceCardLevel; }
		set {
			_iceCardLevel = value;
			if (value > 0) {
				render.sprite = GameDefine.instance.GetCardModuleByType (CardType.ICE).sprite;
			} else {
				render.sprite = GameDefine.instance.GetCardModuleByType (CardType.RED).sprite;
			}
		}
	}

	public SpriteRenderer render;
	CardType _type;

	//
	public CardType type {
		get { return _type; }
		set {
			_type = value;
			cardModule = GameDefine.instance.GetCardModuleByType (value);
		}
	}

	CardModule _cardModule;

	public CardModule cardModule {
		get { return _cardModule; }
		set {
			_cardModule = value;
			//render.sprite = value.sprite;
		}

	}

	void OnMouseDown ()
	{
		// khong lam gi neu card dang lat hoac ko dc lat 
		if (GameplayControl.instance.canFlipCard == false || faceType == CardFaceType.FACE_UP) {
			return;
		}
		// khong lam gi neu dang bi dong bang 
		if (iceCardLevel > 0) {
			return;
		}
		// neu chua bat dau game thi ko lam gi
		if (GameManager.gameState != GameState.PLAYING) {
			return;
		}
		//Debug.Log ("mouse down");
		if (GameplayControl.instance.choosingCard == null) {
			GameplayControl.instance.choosingCard = this;
			faceType = CardFaceType.FACE_UP;
		} else {
			// neu match
			if (GameplayControl.instance.choosingCard.type == type) {
				if (GameplayControl.instance.choosingCard == this) {
					return;
				}
				// tinh toan chien dau 
				faceType = CardFaceType.FACE_UP;
				GameplayControl.instance.canFlipCard = false;
				Invoke ("Match", 1F);
			} else if (GameplayControl.instance.choosingCard.type == CardType.X2) {
				// neu match voi x2
				for (int i = 0; i < GameplayControl.instance.listCardActive.Count; i++) {
					Card _card = GameplayControl.instance.listCardActive [i];
					if (_card.type == type && _card != this) {
						_card.faceType = CardFaceType.FACE_UP;
						faceType = CardFaceType.FACE_UP;
						GameplayControl.instance.canFlipCard = false;
						StartCoroutine (MatchX2 (GameplayControl.instance.choosingCard, this, _card));
						break;
					}
				}
			} else if (type == CardType.X2) {
				// neu x2 match voi card khac
				for (int i = 0; i < GameplayControl.instance.listCardActive.Count; i++) {
					Card _card = GameplayControl.instance.listCardActive [i];
					if (_card.type == GameplayControl.instance.choosingCard.type && _card != GameplayControl.instance.choosingCard) {
						_card.faceType = CardFaceType.FACE_UP;
						faceType = CardFaceType.FACE_UP;
						GameplayControl.instance.canFlipCard = false;
						StartCoroutine (MatchX2 (GameplayControl.instance.choosingCard, this, _card));
						break;
					}
				}
			} else {
				// neu khong match
				faceType = CardFaceType.FACE_UP;
				GameplayControl.instance.canFlipCard = false;
				Invoke ("NotMatch", 1F);
			}

		}
	}

	IEnumerator MatchX2 (Card _card1, Card _card2, Card _card3)
	{
		GameplayControl.instance.isLastTurnMatch = true;
		yield return new WaitForSecondsRealtime (1F);

		GameplayControl.instance.listCardInactive.Add (_card1);
		GameplayControl.instance.listCardInactive.Add (_card2);
		GameplayControl.instance.listCardInactive.Add (_card3);

		GameplayControl.instance.listCardActive.Remove (_card1);
		GameplayControl.instance.listCardActive.Remove (_card2);
		GameplayControl.instance.listCardActive.Remove (_card3);

		_card1.gameObject.SetActive (false);
		_card2.gameObject.SetActive (false);
		_card3.gameObject.SetActive (false);

		GameplayControl.instance.choosingCard = null;

		GameplayControl.instance.canFlipCard = true;
		if (_card1.type != CardType.X2) {
			Attack (_card1.cardModule, true);
		} else if (_card2.type != CardType.X2) {
			Attack (_card2.cardModule, true);
		}


	}

	void Match ()
	{
		GameplayControl.instance.isLastTurnMatch = true;
		GameplayControl.instance.canFlipCard = true;

		// add to inactive list
		GameplayControl.instance.listCardInactive.Add (GameplayControl.instance.choosingCard);
		GameplayControl.instance.listCardInactive.Add (this);
		//
		GameplayControl.instance.listCardActive.Remove (GameplayControl.instance.choosingCard);
		GameplayControl.instance.listCardActive.Remove (this);
		// inactive gameobject and remove choosingCard
		GameplayControl.instance.choosingCard.gameObject.SetActive (false);
		GameplayControl.instance.choosingCard = null;
		gameObject.SetActive (false);
		Attack (cardModule);
		// tinh toan chien dau
	}

	void Attack (CardModule _card, bool x2 = false)
	{

		int damage = _card.value;
		damage = damage + GameManager.dataSave.atkAdd;
		if (x2) {
			damage = damage * 2;
		}
		CardType _type = _card.type;
		switch (_type) {
		case CardType.ARROW_1:
			GameplayControl.instance.bunny.Attack (damage);
			AudioManager.instance.PlaySoundDelay (AudioClipType.player_bow, 1.4F);
			break;
		case CardType.ARROW_2:
			GameplayControl.instance.bunny.Attack (damage);
			AudioManager.instance.PlaySoundDelay (AudioClipType.player_bow, 1.4F);
			break;
		case CardType.ARROW_3:
			GameplayControl.instance.bunny.Attack (damage);
			AudioManager.instance.PlaySoundDelay (AudioClipType.player_bow, 1.4F);
			break;
		case CardType.SWORD_1:
			GameplayControl.instance.wolf.Attack (damage);
			AudioManager.instance.PlaySoundDelay (AudioClipType.player_sword, 1.4F);
			break;
		case CardType.SWORD_2:
			GameplayControl.instance.wolf.Attack (damage);
			AudioManager.instance.PlaySoundDelay (AudioClipType.player_sword, 1.4F);
			break;
		case CardType.SWORD_3:
			GameplayControl.instance.wolf.Attack (damage);
			AudioManager.instance.PlaySoundDelay (AudioClipType.player_sword, 1.4F);
			break;
		case CardType.MAGIC_1:
			GameplayControl.instance.cat.Attack (damage);
			AudioManager.instance.PlaySoundDelay (AudioClipType.player_spell, 1.4F);
			break;
		case CardType.MAGIC_2:
			GameplayControl.instance.cat.Attack (damage);
			AudioManager.instance.PlaySoundDelay (AudioClipType.player_spell, 1.44F);
			break;
		case CardType.MAGIC_3:
			GameplayControl.instance.cat.Attack (damage);
			AudioManager.instance.PlaySoundDelay (AudioClipType.player_spell, 1.4F);
			break;
		case CardType.SWORD_ARROW_MAGIC:
			GameplayControl.instance.bunny.Attack (damage);
			GameplayControl.instance.cat.Attack (damage);
			GameplayControl.instance.wolf.Attack (damage);
			AudioManager.instance.PlaySoundDelay (AudioClipType.player_sword, 1.4F);
			AudioManager.instance.PlaySoundDelay (AudioClipType.player_spell, 1.4F);
			AudioManager.instance.PlaySoundDelay (AudioClipType.player_bow, 1.4F);
			break;
		case CardType.HP:
                //GameplayControl.instance.cat.Attack ();
			GameplayControl.instance.hp += 5;
			GameplayControl.instance.turnCount--;
			//Debug.Log ("Next turn");
			AudioManager.instance.PlaySoundDelay (AudioClipType.player_bottle, 0.1F);
			//AudioManager.instance.PlaySound (AudioClipType.);
			break;
		case CardType.SHIELD:
                //GameplayControl.instance.cat.Attack ();
			int shieldValue = x2 == true ? cardModule.value * 2 : cardModule.value;

			GameplayControl.instance.playerShieldLevel = shieldValue + 1;
			GameplayControl.instance.turnCount--;
			AudioManager.instance.PlaySound (AudioClipType.player_shield);
			//Debug.Log ("Next turn");
			break;

		}

		// kiem tra xem neu khong con card nao thi thua 
		if (GameplayControl.instance.listCardActive.Count <= 0) {
			GameplayControl.instance.hp = 0;
		}
	}

	void NotMatch ()
	{
		GameplayControl.instance.isLastTurnMatch = false;
		GameplayControl.instance.turnCount--;
		Debug.Log ("Next turn");
		GameplayControl.instance.canFlipCard = true;
		GameplayControl.instance.choosingCard.faceType = CardFaceType.FACE_DOWN;
		faceType = CardFaceType.FACE_DOWN;
		GameplayControl.instance.choosingCard = null;
	}

	// hieu ung thay doi vi tri, dung cho skill cua monster 9
	public void ChangePosEffect ()
	{
		GameObject changeIcon = (GameObject)Instantiate (GameDefine.instance.changeIconPrefap, transform.position, Quaternion.Euler (0, 0, -45));

		transform.DOScaleX (0.01F, 0.5F).SetUpdate (true).OnComplete (() => {
			transform.DOScaleX (1F, 0.5F).SetUpdate (true).OnComplete (() => {
				Destroy (changeIcon);
			});
		});
	}


	void Start ()
	{
		// sau 1 giay bat dau scene  thi bat dau tao bo bai ngau nhien
		Invoke ("StartGame", 1F);
	}

	void StartGame ()
	{
		faceType = CardFaceType.FACE_UP;
		// cho xem bo bai trong 3 giay
		Invoke ("DownFace", 3F);
	}

	public void DownFace ()
	{
		faceType = CardFaceType.FACE_DOWN;
	}

}

[System.Serializable]
public enum CardFaceType
{
	FACE_UP,
	FACE_DOWN
}